﻿using OP20.Models;
using Prism.Events;
using QH_MSS_WPF_Core.Models;
using System.Collections.ObjectModel;

namespace OP20.ViewModels
{
    public class ST03ViewModel : BindableBase, INavigationAware
    {
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;

        public ST03ViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<EventWarning>().Subscribe(DoWarnings);
            //开启定时器任务
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval =  TimeSpan.FromMilliseconds(50);
            dispatcherTimer.Tick += OnTick;
            dispatcherTimer.Start();
        }

        private void DoWarnings(ObservableCollection<WarningModel> collection)
        {
            WarningData = collection;

        }
        #region 定时器
        //定时器

        private void OnTick(object? sender, EventArgs e)
        {
            Status = ST3Station_Global.stationPLCToPC.nStationStatus;

            SetStatus();

            //按钮状态
            BtnInitText = ST3Station_Global.stationPLCToPC.bInitialSTing ? "初始化中" : "初始化";
            BtnCycleStartText = ST3Station_Global.stationPLCToPC.bCycleStart ? "单循环启动中" : "单循环启动";
            BtnStationStartText = ST3Station_Global.stationPLCToPC.bSignSTstart ? "单工站启动中" : "单工站启动";

            IsBtnInit = ST3Station_Global.stationPLCToPC.bInitialSTing;
            IsBtnCycleStart = ST3Station_Global.stationPLCToPC.bCycleStart;
            IsBtnStationStart = ST3Station_Global.stationPLCToPC.bSignSTstart;

            //单工站初始化3s
            //if (isBtnInit)
            //{
            //    if (DateTime.Now - btnInitStartTime > TimeSpan.FromSeconds(3))
            //    {
            //        //初始化
            //        ST3Station_Global.stationPCToPLC.bInitialST = true;
            //        isBtnInit = false;
            //    }
            //}
            //气缸状态
            OrgOK = TurntablePLCToPC_Global.OrgOk;
            StationCode = TurntablePLCToPC_Global.StationCode;
            AutoStationCode = TurntablePLCToPC_Global.AutoStationCode;

            //转盘互锁条件
            Bool1 = ST01WeighUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
            Bool2 = ST02TestHighUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
            Bool3 = IO_Global.IO_R[114];

            Bool4 = ST04JackCy.cylinderPLCToPC_Global.bOriginSensor;
            Bool5 = ST05UpDownCy.cylinderPLCToPC_Global.bOriginSensor;
            Bool6 = ST06SpinUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
            Bool7 = ST08JackCy.cylinderPLCToPC_Global.bOriginSensor;
            Bool8 = ST08Door03Cy.cylinderPLCToPC_Global.bOriginSensor;

            Bool9 = ST10UpDownCy.cylinderPLCToPC_Global.bOriginSensor;
            Bool10 = ST11SpinUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
            Bool11 = ST12PressInCy.cylinderPLCToPC_Global.bOriginSensor;
            Bool12 = ST12UpDownCy.cylinderPLCToPC_Global.bOriginSensor;
            Bool13 = ST12SpinUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
            Bool14 = ST13PressInCy.cylinderPLCToPC_Global.bOriginSensor;

            if (ST14CarryUpDown01Cy.cylinderPLCToPC_Global.bOriginSensor||ST14ChuoWei01Cy.cylinderPLCToPC_Global.bOriginSensor||ST14ChuoWei02Cy.cylinderPLCToPC_Global.bOriginSensor)
            {
                Bool15 = true;
            }
            if (ST14CarryUpDown02Cy.cylinderPLCToPC_Global.bOriginSensor || ST14ChuoWei01Cy.cylinderPLCToPC_Global.bOriginSensor || ST14ChuoWei02Cy.cylinderPLCToPC_Global.bOriginSensor)
            {
                Bool16 = true;
            }
        }

        #endregion


        #region 私有方法

        /// <summary>
        /// 设置工站状态
        /// </summary>
        private void SetStatus()
        {
            if (Status == 0)
            {
                StatusText = "准备中";
            }
            else if (Status == 1)
            {
                StatusText = "故障中";

            }
            else if (Status == 2)
            {
                StatusText = "运行中";

            }

            else if (Status == 4)
            {
                StatusText = "单循环启动";

            }

            else if (Status == 5)
            {
                StatusText = "单工站启动";
            }
            else if (Status == 9)
            {
                StatusText = "未使能";
            }
        }



        #endregion


        #region 数据
        private ObservableCollection<WarningModel> _WarningData;

        public ObservableCollection<WarningModel> WarningData
        {
            get { return _WarningData ?? (_WarningData = new ObservableCollection<WarningModel>()); }
            set { SetProperty(ref _WarningData, value); }
        }

        private ObservableCollection<CylinderModel> _QGList;
        public ObservableCollection<CylinderModel> QGList
        {
            get { return _QGList ?? (_QGList = new ObservableCollection<CylinderModel>()); }
            set { SetProperty(ref _QGList, value); }
        }


        private string _StationName;
        public string StationName
        {
            get { return _StationName; }
            set { SetProperty(ref _StationName, value); }
        }

        private string _NavigateFrom;
        public string NavigateFrom
        {
            get { return _NavigateFrom; }
            set { SetProperty(ref _NavigateFrom, value); }
        }



        private int _Status;
        public int Status
        {
            get { return _Status; }
            set { SetProperty(ref _Status, value); }
        }

        private string _StatusText;
        public string StatusText
        {
            get { return _StatusText; }
            set { SetProperty(ref _StatusText, value); }
        }

        private string _BtnInitText;
        public string BtnInitText
        {
            get { return _BtnInitText; }
            set { SetProperty(ref _BtnInitText, value); }
        }

        private string _BtnCycleStartText;
        public string BtnCycleStartText
        {
            get { return _BtnCycleStartText; }
            set { SetProperty(ref _BtnCycleStartText, value); }
        }

        private string _BtnStationStartText;
        public string BtnStationStartText
        {
            get { return _BtnStationStartText; }
            set { SetProperty(ref _BtnStationStartText, value); }
        }


        private bool _IsBtnInit;
        public bool IsBtnInit
        {
            get { return _IsBtnInit; }
            set { SetProperty(ref _IsBtnInit, value); }
        }

        private bool _IsBtnCycleStart;
        public bool IsBtnCycleStart
        {
            get { return _IsBtnCycleStart; }
            set { SetProperty(ref _IsBtnCycleStart, value); }
        }


        private bool _IsBtnStationStart;
        public bool IsBtnStationStart
        {
            get { return _IsBtnStationStart; }
            set { SetProperty(ref _IsBtnStationStart, value); }
        }


        private bool _OrgOK;
        public bool OrgOK
        {
            get { return _OrgOK; }
            set { SetProperty(ref _OrgOK, value); }
        }


        private byte _StationCode;
        public byte StationCode
        {
            get { return _StationCode; }
            set { SetProperty(ref _StationCode, value); }
        }


        private byte _AutoStationCode;
        public byte AutoStationCode
        {
            get { return _AutoStationCode; }
            set { SetProperty(ref _AutoStationCode, value); }
        }

        private short _EncodeAngle;
        public short EncodeAngle
        {
            get { return _EncodeAngle; }
            set { SetProperty(ref _EncodeAngle, value); }
        }

        #region 转盘互锁条件
        private bool _Bool1;
        public bool Bool1
        {
            get { return _Bool1; }
            set { SetProperty(ref _Bool1, value); }
        }


        private bool _Bool2;
        public bool Bool2
        {
            get { return _Bool2; }
            set { SetProperty(ref _Bool2, value); }
        }


        private bool _Bool3;
        public bool Bool3
        {
            get { return _Bool3; }
            set { SetProperty(ref _Bool3, value); }
        }

        private bool _Bool4;
        public bool Bool4
        {
            get { return _Bool4; }
            set { SetProperty(ref _Bool4, value); }
        }


        private bool _Bool5;
        public bool Bool5
        {
            get { return _Bool5; }
            set { SetProperty(ref _Bool5, value); }
        }

        private bool _Bool6;
        public bool Bool6
        {
            get { return _Bool6; }
            set { SetProperty(ref _Bool6, value); }
        }


        private bool _Bool7;
        public bool Bool7
        {
            get { return _Bool7; }
            set { SetProperty(ref _Bool7, value); }
        }

        private bool _Bool8;
        public bool Bool8
        {
            get { return _Bool8; }
            set { SetProperty(ref _Bool8, value); }
        }

        private bool _Bool9;
        public bool Bool9
        {
            get { return _Bool9; }
            set { SetProperty(ref _Bool9, value); }
        }

        private bool _Bool10;
        public bool Bool10
        {
            get { return _Bool10; }
            set { SetProperty(ref _Bool10, value); }
        }

        private bool _Bool11;
        public bool Bool11
        {
            get { return _Bool11; }
            set { SetProperty(ref _Bool11, value); }
        }

        private bool _Bool12;
        public bool Bool12
        {
            get { return _Bool12; }
            set { SetProperty(ref _Bool12, value); }
        }

        private bool _Bool13;
        public bool Bool13
        {
            get { return _Bool13; }
            set { SetProperty(ref _Bool13, value); }
        }


        private bool _Bool14;
        public bool Bool14
        {
            get { return _Bool14; }
            set { SetProperty(ref _Bool14, value); }
        }

        private bool _Bool15;
        public bool Bool15
        {
            get { return _Bool15; }
            set { SetProperty(ref _Bool15, value); }
        }

        private bool _Bool16;
        public bool Bool16
        {
            get { return _Bool16; }
            set { SetProperty(ref _Bool16, value); }
        }

        private bool _Bool17;
        public bool Bool17
        {
            get { return _Bool17; }
            set { SetProperty(ref _Bool17, value); }
        }
        #endregion
        #endregion




        #region 按钮命令


        private DelegateCommand _GoBackCommand;
        public DelegateCommand GoBackCommand =>
            _GoBackCommand ?? (_GoBackCommand = new DelegateCommand(ExecuteGoBackCommand));

        void ExecuteGoBackCommand()
        {
            if (!String.IsNullOrEmpty(NavigateFrom))
            {
                _regionManager.RequestNavigate(PrismManager.MainViewRegionName, NavigateFrom);
                return;
            }

            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.MenuView);


        }

        private DelegateCommand<string> _QGDownCommand;
        public DelegateCommand<string> QGDownCommand =>
            _QGDownCommand ?? (_QGDownCommand = new DelegateCommand<string>(ExecuteQGDownCommand));

        void ExecuteQGDownCommand(string parameter)
        {
            switch (parameter)
            {

            }
        }


        private DelegateCommand<string> _QGUpCommand;
        public DelegateCommand<string> QGUpCommand =>
            _QGUpCommand ?? (_QGUpCommand = new DelegateCommand<string>(ExecuteQGUpCommand));

        void ExecuteQGUpCommand(string parameter)
        {
            switch (parameter)
            {


            }
        }
        private DelegateCommand<string> _NavigateCommand;
        public DelegateCommand<string> NavigateCommand =>
            _NavigateCommand ?? (_NavigateCommand = new DelegateCommand<string>(ExecuteNavigateCommand));

        void ExecuteNavigateCommand(string parameter)
        {
            switch (parameter)
            {
                case "QGPingBI":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST03CyManagerView);
                    break;
                case "DeviceManage":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST03DeviceManagerView);
                    break;
            }

        }


        private DelegateCommand _GoHomeDownCmd;
        public DelegateCommand GoHomeDownCmd =>
            _GoHomeDownCmd ?? (_GoHomeDownCmd = new DelegateCommand(ExecuteGoHomeDownCmd));

        void ExecuteGoHomeDownCmd()
        {
            OtherDevicePCToPLC_Global.bHomeBT = true;
        }
        private DelegateCommand _GoHomeUpCmd;
        public DelegateCommand GoHomeUpCmd =>
            _GoHomeUpCmd ?? (_GoHomeUpCmd = new DelegateCommand(ExecuteGoHomeUpCmd));

        void ExecuteGoHomeUpCmd()
        {
            OtherDevicePCToPLC_Global.bHomeBT = false;

        }

        private DelegateCommand _JogDownCmd;
        public DelegateCommand JogDownCmd =>
            _JogDownCmd ?? (_JogDownCmd = new DelegateCommand(ExecuteJogDownCmd));

        void ExecuteJogDownCmd()
        {
            OtherDevicePCToPLC_Global.bJogBT = true;

        }

        private DelegateCommand _JogUpCmd;
        public DelegateCommand JogUpCmd =>
            _JogUpCmd ?? (_JogUpCmd = new DelegateCommand(ExecuteJogUpCmd));

        void ExecuteJogUpCmd()
        {
            OtherDevicePCToPLC_Global.bJogBT = false;

        }
        #endregion

        #region 单工站初始化/单循环启动/单工站启动


        /*
         * 初始化按钮必须按下3s才会起效果，防止误操作
         */
        private bool isBtnInit = false;
        private DateTime btnInitStartTime = DateTime.Now;

        private DelegateCommand _BtnInitMouseDownCmd;
        public DelegateCommand BtnInitMouseDownCmd =>
            _BtnInitMouseDownCmd ?? (_BtnInitMouseDownCmd = new DelegateCommand(ExecuteBtnInitMouseDownCmd));

        void ExecuteBtnInitMouseDownCmd()
        {
            //btnInitStartTime = DateTime.Now;
            //isBtnInit = true;
            ST3Station_Global.stationPCToPLC.bInitialST = true;

        }


        private DelegateCommand _BtnInitMouseUpCmd;
        public DelegateCommand BtnInitMouseUpCmd =>
            _BtnInitMouseUpCmd ?? (_BtnInitMouseUpCmd = new DelegateCommand(ExecuteBtnInitMouseUpCmd));

        void ExecuteBtnInitMouseUpCmd()
        {
            ST3Station_Global.stationPCToPLC.bInitialST = false;
            //  isBtnInit = false;
        }

        private DelegateCommand _BtnCycleStartMouseDownCmd;
        public DelegateCommand BtnCycleStartMouseDownCmd =>
            _BtnCycleStartMouseDownCmd ?? (_BtnCycleStartMouseDownCmd = new DelegateCommand(ExecuteBtnCycleStartMouseDownCmd));

        void ExecuteBtnCycleStartMouseDownCmd()
        {
            ST3Station_Global.stationPCToPLC.bCycleStartBT = true;
        }

        private DelegateCommand _BtnCycleStartMouseUpCmd;
        public DelegateCommand BtnCycleStartMouseUpCmd =>
            _BtnCycleStartMouseUpCmd ?? (_BtnCycleStartMouseUpCmd = new DelegateCommand(ExecuteBtnCycleStartMouseUpCmd));

        void ExecuteBtnCycleStartMouseUpCmd()
        {
            ST3Station_Global.stationPCToPLC.bCycleStartBT = false;

        }

        private DelegateCommand _BtnStationStartMouseDownCmd;
        public DelegateCommand BtnStationStartMouseDownCmd =>
            _BtnStationStartMouseDownCmd ?? (_BtnStationStartMouseDownCmd = new DelegateCommand(ExecuteBtnStationStartMouseDownCmd));

        void ExecuteBtnStationStartMouseDownCmd()
        {
            ST3Station_Global.stationPCToPLC.bSignSTstartBT = true;

        }

        private DelegateCommand _BtnStationStartMouseUpCmd;
        public DelegateCommand BtnStationStartMouseUpCmd =>
            _BtnStationStartMouseUpCmd ?? (_BtnStationStartMouseUpCmd = new DelegateCommand(ExecuteBtnStationStartMouseUpCmd));

        void ExecuteBtnStationStartMouseUpCmd()
        {
            ST3Station_Global.stationPCToPLC.bSignSTstartBT = false;

        }
        #endregion

        #region 导航

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            StationName = navigationContext.Parameters.GetValue<string>("StationName");
            NavigateFrom = navigationContext.Parameters.GetValue<string>("NavigateFrom");
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        #endregion
    }
}
